package xyz.luxiaoxiang.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.luxiaoxiang.bean.HotQuestion;
import xyz.luxiaoxiang.dao.HotQuestionDao;

import java.util.List;

/**
 * 知乎服务
 *
 * @author luxx
 * @date 2018/5/29 0029
 */
@Service
public class ZhihuService {

    /**
     * 查询关键字
     */
    public String queryContext(String keyword) {
        List<HotQuestion> questions = hotQuestionDao.findByExcerptLike("%" + keyword + "%");
        if (questions == null || questions.size() == 0) {
            return "未能查询到相关内容";
        } else {
            StringBuffer sb = new StringBuffer("为你找到以下内容:");
            int i = 0;
            for (HotQuestion question : questions) {
                sb.append("\n").append(link("http://www.luxiaoxiang.xyz/spider2/getAnswerById?answer_id=" + question.getAnswerId(), question.getQuestionName()));
                i++;
                if (i >= 5) {
                    return sb.toString();
                }
            }
            return sb.toString();
        }
    }


    public static String link(String url, String text) {
        return "<a href='" + url + "'>" + text + "</a>";
    }

    @Autowired
    private HotQuestionDao hotQuestionDao;
}
